package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.PropValue;
import com.fadadianqi.goods.model.Properties;
import com.fadadianqi.goods.service.GoodsService;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Controller
public class PropValueController {

    @RequestMapping(value = "/a/brand-category/props-value/form")
    public String ajaxForm(HttpServletRequest request, ModelMap modelMap) {
        String propId = ParamUtils.getString(request, "pid", null);
        modelMap.put("pid", propId);
        String valueId = ParamUtils.getString(request, "vid", null);
        modelMap.put("vid", valueId);
        if (propId != null) {
            Properties prop = goodsService.getProperties(propId);
            modelMap.put("properties", prop);
            Category category = goodsService.getCategoryById(prop.getCategoryId());
            modelMap.put("category", category);
        }
        if (valueId != null) {
            PropValue v = goodsService.getPropValue(valueId);
            modelMap.put("propValue", v);
        }
        return "/admin/brand-category/propsValueForm";
    }

    @RequestMapping(value = "/a/brand-category/props-value/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> ajaxSave(HttpServletRequest request, ModelMap modelMap) {
        String propId = ParamUtils.getString(request, "pid", null);
        String valueId = ParamUtils.getString(request, "vid", null);
        String propValueName = ParamUtils.getString(request, "vname", null);
        PropValue v = new PropValue();
        v.setId(valueId);
        v.setName(propValueName);
        v.setPropId(propId);
        //
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        try {
            goodsService.savePropValue(v);
            AjaxResult rs = new AjaxResult(v, Boolean.TRUE, null);
            return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                    responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            String msg = messageSource.getMessage(e.getMessage(), null, Locale.getDefault());
            AjaxResult rs = new AjaxResult(null, Boolean.FALSE, msg);
            return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                    responseHeaders, HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/a/brand-category/props-value/del", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> ajaxDel(HttpServletRequest request, ModelMap modelMap) {
        String valueId = ParamUtils.getString(request, "vid", null);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        try {
            goodsService.removePropValueById(valueId);
            AjaxResult rs = new AjaxResult(null, Boolean.TRUE, null);
            return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                    responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            String msg = messageSource.getMessage(e.getMessage(), null, Locale.getDefault());
            AjaxResult rs = new AjaxResult(null, Boolean.FALSE, msg);
            return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                    responseHeaders, HttpStatus.OK);
        }
    }

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private GoodsService goodsService;
}
